package zstack

import (
	. "bc/util"
	"encoding/json"
	"fmt"
)

type zone struct {

}
func (t *zone) Create (d map[string]interface{}) (string,error){

	oauth ,err := Oauth()
	if err != nil{
		return "",err
	}
	jsonStr,err := json.Marshal(d)
	if err != nil {
		return "",err
	}
	url := Config.Zstack.ApiUrl + "v1/zones"

	result,err := Curl.Header(map[string]string{
		"Content-Type":"application/json;charset=UTF-8",
		"Authorization":"OAuth " + oauth,
		"X-Web-Hook":Config.Zstack.WebHook + "zone_add_call_back",
	}).Body(string(jsonStr)).Url(url).POST()

	if err != nil{
		return "",err
	}
	return result,nil
}
func (t *zone)Delete(uuid string,dMode string,) (string,error){

	oauth ,err := Oauth()
	if err != nil{
		return "",err
	}

	url := Config.Zstack.ApiUrl + "v1/zones/%s?deleteMode=%s"
	url = fmt.Sprintf(url,uuid,dMode)

	result,err := Curl.Header(map[string]string{
		"Content-Type":"application/json;charset=UTF-8",
		"Authorization":"OAuth " + oauth,
		"X-Web-Hook":Config.Zstack.WebHook + "zone_del_call_back?uuid=" + uuid,
	}).Url(url).DELETE()

	if err != nil {
		return "",err
	}
	return result,nil

}
func (t *zone)Update(uuid string,body string)(string,error) {

	oauth ,err := Oauth()
	if err != nil{
		return "",err
	}
	url := Config.Zstack.ApiUrl + "v1/zones/%s/actions"
	url = fmt.Sprintf(url,uuid)
	result,err := Curl.Header(map[string]string{
		"Content-Type":"application/json;charset=UTF-8",
		"Authorization":"OAuth " + oauth,
		"X-Web-Hook":Config.Zstack.WebHook + "zone_upd_call_back?uuid=" + uuid,
	}).Url(url).Body(body).PUT()
	if err != nil {
		return "",err
	}
	return result,nil

}
func (t *zone)ChangeZoneState(uuid string,body string)(string,error){

	oauth ,err := Oauth()
	if err != nil{
		return "",err
	}
	url := Config.Zstack.ApiUrl + "v1/zones/%s/actions"
	url = fmt.Sprintf(url,uuid)
	result,err := Curl.Header(map[string]string{
		"Content-Type":"application/json;charset=UTF-8",
		"Authorization":"OAuth " + oauth,
		"X-Web-Hook":Config.Zstack.WebHook + "zone_upd_state_call_back?uuid=" + uuid,
	}).Url(url).Body(body).PUT()
	if err != nil {
		return "",err
	}
	return result,nil

}
var Zone = &zone{}
